﻿using Newtonsoft.Json;
using SK3000RunningTestTool.SKServerDelegate;
using SK3000RunningTestTool.SKServerEnum;
using SK3000RunningTestTool.SKServerSource;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace SK3000RunningTestTool.SKService
{
    public class Service_WebService : ISKService
    {
        public SKServer Server
        {
            get;
            set;
        }
        public int ID
        {
            get;
            set;
        }

        private Enum_ServiceTestType typeID = Enum_ServiceTestType.WebService;
        /// <summary>
        /// 类型ID
        /// </summary>
        public Enum_ServiceTestType TypeID
        {
            get { return typeID; }
            set { }
        }
        public ServiceTestType Type => ServiceTestTypeSource.ServiceTestTypeList[typeID];

        string strServiceName = "Web后台服务";
        public string ServiceName
        {
            get { return strServiceName; }
            set { strServiceName = value; }
        }

        public string ServiceRemark
        {
            get;
            set;
        }
        public string ServiceAddress
        {
            get;
            set;
        }

        public ushort ServicePort
        {
            get;
            set;
        }

        public object Tag
        {
            get;
            set;
        }
        

        public event ServiceTestProgressDelegate ServiceTestProgressEvent;

        private void ServiceTestProgress(ServiceTestProgressValue value)
        {
            if (ServiceTestProgressEvent != null)
            {
                ServiceTestProgressEvent(this, value);
            }
        }



        public event ServiceTestCallbackDelegate ServiceTestCallbackEvent;

        public void ServiceTestCallback(ServiceTestCallbackValue value)
        {
            if (ServiceTestCallbackEvent != null)
            {
                ServiceTestCallbackEvent(this, value);
            }
        }

        /// <summary>
        /// 服务器测试 
        /// </summary>
        /// <returns></returns>
        public ServiceTestCompleteValue ServiceTest()
        {

            ServiceTestCallback(new ServiceTestCallbackValue { EventValue = Enum_ServiceTestEvent.ServiceTestStart });
            ServiceTestCallback(new ServiceTestCallbackValue { EventValue = Enum_ServiceTestEvent.ConnectServiceing });
            ServiceTestProgress(new ServiceTestProgressValue { ProgressValue = 0 });
            ServiceTestCompleteValue result = new ServiceTestCompleteValue
            {
                reuslt = Enum_ServiceTestResult.Normal,
            };
            try
            {
                ServiceTestProgress(new ServiceTestProgressValue { ProgressValue = 10 });
                string Temp_strUrl = ServiceAddress;
                if (ServicePort != 80 && ServicePort != 443)
                {
                    Temp_strUrl += ":" + ServicePort;
                }
                var strResult = CommonMethod.Common_Web.HttpGet(Temp_strUrl + "/SK3000WebServiceCore/api/Group/GetGroupInfo?token=1&groupId=1", "");
                SKWebDataInterFace.SKJsonResult Temp_result = JsonConvert.DeserializeObject<SKWebDataInterFace.SKJsonResult>(strResult);

                ServiceTestProgress(new ServiceTestProgressValue { ProgressValue = 60 });
                if (Temp_result.StateCode != SKWebDataInterFace.StateCode.OK)
                {
                    result.reuslt = Enum_ServiceTestResult.Fail;
                    switch (Temp_result.StateCode)
                    {
                        case SKWebDataInterFace.StateCode.Error:
                            result.Para1 = Temp_result.Content;
                            break;
                        case SKWebDataInterFace.StateCode.Fail:
                            result.Para1 = "数据异常";
                            break;
                        case SKWebDataInterFace.StateCode.DataNull:
                            result.Para1 = "空数据";
                            break;
                        case SKWebDataInterFace.StateCode.TokenExpire:
                            result.Para1 = "Token异常";
                            break;
                        default:
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                result.reuslt = Enum_ServiceTestResult.Fail;
                result.Para1 = ex.ToString();
            }
            finally
            {
                ServiceTestCallback(new ServiceTestCallbackValue { EventValue = Enum_ServiceTestEvent.ConnectServiced });
                ServiceTestProgress(new ServiceTestProgressValue { ProgressValue = 80 });
            }
            
            ServiceTestProgress(new ServiceTestProgressValue { ProgressValue = 100 });
            ServiceTestCallback(new ServiceTestCallbackValue { EventValue = Enum_ServiceTestEvent.ServiceTestEnd });
            return result;
        }
        public string HttpGet(string Url)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            request.Method = "GET";
            request.ContentType = "text/html;charset=UTF-8";
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();
            return retString;
        }
        public void ServiceTestAsync(ServiceTestCompleteDelegate callback)
        {
            Timer timTimeOutVideoClose = new Timer(new TimerCallback(timTimtOutVideoClose_Event), callback, 0, Timeout.Infinite);
            return;
        }

        private void timTimtOutVideoClose_Event(object sender)
        {
            ServiceTestCompleteValue value = ServiceTest();
            ServiceTestCompleteDelegate call = (ServiceTestCompleteDelegate)sender;
            if (call != null)
            {
                call(this, value);
            }
        }
    }
}
